﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using NENAData.LINQ;
using NENAData.Payments;
using PayPal.API;
using NENAData.DLC;
using NENAData.Factory;

namespace PayPal
{
    public partial class ViewTransactionReciept : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BindRecieptData();
            //Response.Redirect("popup.aspx","_blank");            
        }


        private void BindRecieptData()
        {
            try
            {

            int intTransactionID = Convert.ToInt32(Request.QueryString["TransactionID"]);
            int intMemberID = Convert.ToInt32(Request.QueryString["Member_ID"]);
            
            //NENAPayments oPayment = new NENAPayments();
               // oPayment.GetTransactionPayment(intTransactionID, intMemberID);
            //intTransactionID = 3440;
            //intMemberID = 1050;

            MembershipDataContext db = GetTransaction();

            var oTransaction = (from m in db.Members
                                join transactionPayment in db.TransactionPayments on m.Member_ID equals
                                    transactionPayment.Member_ID
                                join transaction in db.Transactions on transactionPayment.TransactionID_NENA equals
                                    transaction.TransactionID_NENA
                                join affiliate in db.Affiliates on transactionPayment.PaymentTo equals
                                    affiliate.Affiliate_ID
                                where m.Member_ID == intMemberID &&
                                      transactionPayment.TransactionPaymentID == intTransactionID &&
                                      transactionPayment.TransactionCompleted
                                select new
                                           {
                                               m,
                                               transactionPayment,
                                               transaction,
                                               affiliate
                                           }).Single();

                lblTransactionDate.Text = String.Format("{0:d}", oTransaction.transactionPayment.PaymentDate.Value);
                lblPaymentAmount.Text = String.Format("{0:C}", oTransaction.transactionPayment.PaymentAmount.Value);
                lblPaidby.Text = oTransaction.transactionPayment.PaymentType;

                lblPaymentTo.Text = oTransaction.affiliate.Affiliate_Full;

                lblPaymentFor.Text = oTransaction.transaction.TransactionName;

                lblMemberName.Text = oTransaction.m.FirstName + " " + oTransaction.m.LastName;
                lblProvince.Text = oTransaction.m.Province;
                lblPostalCode.Text = oTransaction.m.PostalCode;
                lblCity.Text = oTransaction.m.City;
                lblAddress1.Text = oTransaction.m.Address;
                lblAddress2.Text = oTransaction.m.Address2;

            }
            catch (Exception ex)
            {
                Response.Write("Invalid Reciept <br /> Error:" + ex.Message);
            }

        }

        private MembershipDataContext GetTransaction()
        {
            var x = new SQLMembership();
            return x.GetMembershipDB();
        }

        //todo: future: create MemberReciept object
        //private object GetRecieptData()
        //{
        //    int intTransactionID = 15;

        //    MembershipDataContext db = GetTransaction();

        //    var oTransaction = (from m in db.Members
        //                        where m.Member_ID == 327
        //                        select new
        //                        {
        //                            m.FirstName,
        //                            m.LastName,
        //                            m.Address,
        //                            m.City,
        //                            m.Province,
        //                            m.PostalCode,
        //                            TransactionPayments =
        //                 from tpay in db.TransactionPayments
        //                 where tpay.TransactionPaymentID == intTransactionID
        //                 select new
        //                 {
        //                     tpay.PaymentAmount,
        //                     tpay.PaymentDate,
        //                     tpay.Member_ID
        //                 }
        //                        }).SingleOrDefault();

        //    return oTransaction;
        //}
    }
}
